package Controls;

import Core.*;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class Text extends Control
{	
	public final static int STYLE_FIX_WIDTH=0, STYLE_UNFIX_WIDTH=1, STYLE_MULTILINE=2;
	
	private String strings[];
	private int style;
	private Font font = Font.getDefaultFont();
	
	public int spacing = 1;
	public int align = Graphics.LEFT;
	public int color = 0xffffff;
	public int bgcolor = -1;

	public Text(String text)
	{
		style = STYLE_UNFIX_WIDTH;
		caption = text;
		selectable = false;
		
		init();
	}
	
	public Text(String text, int w, boolean ml)
	{
		if(ml) style = STYLE_MULTILINE;
		else style = STYLE_FIX_WIDTH;
		caption = text;
		W = w;
		selectable = false;
		
		init();
	}

	public Text(String text, int w, int color, boolean ml)
	{
		if(ml) style = STYLE_MULTILINE;
		else style = STYLE_FIX_WIDTH;
		caption = text;
		W = w;
		selectable = false;
		this.color = color;

		init();
	}
	
	private void init()
	{
		if(style==STYLE_FIX_WIDTH || style==STYLE_UNFIX_WIDTH)
		{
			strings = new String[1];
			strings[0] = caption;
		}
		else
		{
			if (caption.equals("") || font == null || W == 0)
			{
				strings = new String[1];
				strings[0] = "";
			}

			Vector stringsV = new Vector();
			int lastDivPos = 0, lastSpacePos = 0, strw;
			char c;

			for (int i = 0; i < caption.length(); i++)
			{
				c = caption.charAt(i);
				if (c == '|')
				{
					strw = font.stringWidth(caption.substring(lastDivPos, i));
					if (strw > (W))
					{
						stringsV.addElement(new String(caption.substring(lastDivPos, lastSpacePos)));
						//System.out.println("** adding '"+caption.substring(lastDivPos, lastSpacePos)+"'");
						lastDivPos = lastSpacePos;
						lastSpacePos = i + 1;
					}

					i += 1;
					stringsV.addElement(new String(caption.substring(lastDivPos, i - 1)));
					//System.out.println("** adding '"+caption.substring(lastDivPos, i-1)+"'");
					lastDivPos = i;
					lastSpacePos = i;
				}
				if (c == ' ' || c == ',' || i == caption.length() - 1)
				{
					strw = font.stringWidth(caption.substring(lastDivPos, i));
					if (strw > (W))
					{
						stringsV.addElement(new String(caption.substring(lastDivPos, lastSpacePos+1)));
						//System.out.println("** adding '"+caption.substring(lastDivPos, lastSpacePos)+"'");
						lastDivPos = lastSpacePos;
					}
					lastSpacePos = i;
				}
			}

			stringsV.addElement(new String(caption.substring(lastDivPos, caption.length())));
			//System.out.println("** adding '"+caption.substring(lastDivPos, caption.length())+"'");

			strings = new String[stringsV.size()];

			for (int i = 0; i < stringsV.size(); i++)
			{
				strings[i] = (String) stringsV.elementAt(i);
				if(strings[i].charAt(0) == ' ' || strings[i].charAt(0) == ',') strings[i] = strings[i].substring(1);
			}
		}
	}

	public void setCaption(String caption)
	{
		setText(caption);
	}

	public void setText(String s)
	{
		caption = s;
		init();
	}

	public void draw(Graphics g, int x, int y)
	{
		super.draw(g, x, y);

		g.setColor(color);
		int s=0;
		
		if(style == STYLE_UNFIX_WIDTH || style == STYLE_MULTILINE)
		{
			if(bgcolor > 0)
			{
				g.setColor(bgcolor);
				g.fillRect(x-1, y, getW()+2, getH()-1);
			}

			g.setColor(color);
			for(int i = 0; i < strings.length; i++)
			{
				g.drawString(strings[i], x, y+s, Graphics.LEFT | Graphics.TOP);
				s += font.getHeight() + spacing;
			}
		}
		else
		{
			int sx=0;

			if (font.stringWidth(strings[0]) <= W - 6)
			{	
				if (align == Graphics.HCENTER)
				{
					sx = (W - font.stringWidth(strings[0])) / 2;
				}

				if(bgcolor > 0)
				{
					g.setColor(bgcolor);
					g.fillRect(x + sx -1, y, font.stringWidth(strings[0])+2, getH()-1);
				}

				g.setColor(color);
				g.drawString(strings[0], x + sx, y, Graphics.LEFT | Graphics.TOP);
			}
			else
			{
				String disp = strings[0];

				for (int i = strings[0].length(); i >= 0; i--)
				{
					disp = strings[0].substring(0, i);
					if (font.stringWidth(disp + "...") <= (W-spacing*4))
					{
						break;
					}
				}

				disp += "...";
				
				if (align == Graphics.HCENTER)
				{
					sx = (W - font.stringWidth(disp)) / 2;
				}

				if(bgcolor > 0)
				{
					g.setColor(bgcolor);
					g.fillRect(x+sx-1, y, font.stringWidth(disp)+2, getH()-1);
				}

				g.setColor(color);
				g.drawString(disp, x + sx, y, Graphics.LEFT | Graphics.TOP);
			}
		}
	}

	public int getW()
	{
		if(style == STYLE_FIX_WIDTH || style == STYLE_MULTILINE) return W;
		else return font.stringWidth(strings[0]);
	}

	public int getH()
	{
		return strings.length*font.getHeight() + spacing*(strings.length-1);
	}
}
